package com.ben.multiple.elasticsearch.template;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClients;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.MultiGetItem;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class JavaBaseTest {
    static ElasticsearchTemplate template;
    static ElasticsearchClient client;

    static {
        client = ElasticsearchClients.createImperative(ClientConfiguration.create("192.168.229.141:9200"));
        template = new ElasticsearchTemplate(client);
    }

    public static void main(String[] args) {
//        delete();
        add();
//        update();
//        allUpdate();
//        get();
        multiGet();
        client.shutdown();
        System.exit(5);
    }

    static void add() {
        Map<String, Object> o = new HashMap<>();
        o.put("address", "湖北省武汉市江岸区解放大道2060号");
        o.put("age", 22);
        o.put("book", "永乐大典111");
        o.put("name", "李四");
        o.put("sex", 2);

        Document document = Document.from(o);
        UpdateQuery updateQuery = UpdateQuery.builder("1").withDocument(document).withDocAsUpsert(true).build();
        template.update(updateQuery, IndexCoordinates.of("es_db5"));
    }

    static void delete() {
        String s = template.delete("1", IndexCoordinates.of("es_db5"));
        System.out.println(s);
    }

    static void update() {
        // 增量修改
        Map<String, Object> o = new HashMap<>();
        o.put("address", "湖北省武汉市江岸区解放大道2060号qqqqqqqqqqqqqqqqqqqqqq");
        o.put("name", "张三");

        Document document = Document.from(o);
        UpdateQuery updateQuery = UpdateQuery.builder("2").withDocument(document).withDocAsUpsert(true).build();
        template.update(updateQuery, IndexCoordinates.of("es_db5"));
    }

    static void allUpdate() {
        // 全量修改 todo
        Map<String, Object> o = new HashMap<>();
        o.put("address", "湖北省武汉市江岸区解放大道2060号qqqqqqqqqqqqqqqqqqqqqq");
        o.put("name", "张三");
        o.put("age", null);

        Document document = Document.from(o);

        UpdateQuery updateQuery = UpdateQuery.builder("2").withDocument(document).build();
        template.update(updateQuery, IndexCoordinates.of("es_db5"));
    }

    static void get() {
        HashMap map = template.get("2", HashMap.class, IndexCoordinates.of("es_db5"));
        System.out.println(map);
    }

    static void multiGet(){
        Query query = Query.multiGetQuery(new ArrayList<>(Arrays.asList("1", "2")));
        List<MultiGetItem<HashMap>> list = template.multiGet(query, HashMap.class, IndexCoordinates.of("es_db5"));
        for (MultiGetItem<HashMap> obj : list) {
            HashMap item = obj.getItem();
            System.out.println(item);
        }
    }

}
